#include "MTXVector2.h"
using namespace MTX;

float Vector2::operator [](int i) const
{
	assert(i<2);
	return *(&x+i);
}

bool Vector2::operator ==(const Vector2& w) const
{
	return x==w.x&&y==w.y;
}

Vector2 Vector2::operator- () const
{
	return Vector2(-x,-y);
}

Vector2 Vector2::operator+ (const Vector2& w) const
{
	return Vector2(x+w.x,y+w.y);
}
Vector2 Vector2::operator- (const Vector2& w) const
{
	return Vector2(x-w.x,y-w.y);
}

float Vector2::operator* (const Vector2& w) const
{
	return x*w.x+y*w.y;
}

Vector2 Vector2::operator* (const float Scalar) const
{
	return Vector2(x*Scalar,y*Scalar);
}

Vector2 Vector2::operator/ (const float Scalar) const
{
	assert(x!=0.0f);
	return Vector2(x/Scalar,y/Scalar);
}

Vector2 Vector2::operator/ (const Vector2& w) const
{
	assert(w.x!=0.0f&&w.y!=0.0f);
	return Vector2(x/w.x,y/w.y);
}

Vector2& Vector2::operator+= (const Vector2& w)
{
	x+=w.x;
	y+=w.y;
	return *this;
}

Vector2& Vector2::operator-= (const Vector2& w)
{
	x-=w.x;
	y-=w.y;
	return *this;
}

Vector2& Vector2::operator*= (const Vector2& w)
{
	x*=w.x;
	y*=w.y;
	return *this;
}

Vector2& Vector2::operator*= (const float Scalar)
{
	x*=Scalar;
	y*=Scalar;
	return *this;
}

Vector2& Vector2::operator/= (const Vector2& w)
{
	assert(w.x!=0.0f&&w.y!=0.0f);
	x/=w.x;
	y/=w.y;
	return *this;
}

Vector2& Vector2::operator/= (const float Scalar)
{
	assert(Scalar!=0.0f);
	x/=Scalar;
	y/=Scalar;
	return *this;
}

Vector2 Vector2::Cross(const Vector2& w) const
{
	return Vector2(x*w.x,y*w.y);
}

float Vector2::Dot(const Vector2& w) const
{
	return x*w.x+y*w.y;
}

float Vector2::Length() const
{
	return sqrt(x*x+y*y);
}

bool Vector2::Normalized() const
{
	return Length()==1.0f;
}

void Vector2::Normalize()
{
	float len = Length();
	assert(len>0.0f);
	x/=len;
	y/=len;
}

float Vector2::Distance(const Vector2& w) const
{
	return (*this-w).Length();
}

bool Vector2::IsZeroLength() const
{
	return Length()==0.0f;
}
